<template>
  <div class="contain">
    <div class="header">
      <el-input
        v-model="input"
        placeholder="请输入菜品名称"
        clearable
        @keyup.enter.native="getList"
      >
        <i
          slot="prefix"
          class="el-input__icon el-icon-search"
          style="cursor: pointer"
          @click="getList"
        />
      </el-input>
      <div class="header-right">
        <el-link type="danger" @click="deleHandle('批量', null)"
          >批量删除</el-link
        >
        <el-divider direction="vertical" />
        <el-link type="primary" @click="statusHandle('1')">批量启售</el-link>
        <el-divider direction="vertical" />
        <el-link type="danger" @click="statusHandle('0')">批量停售</el-link>
        <el-button type="warning" @click="$router.push({ name: 'AddDish' })"
          >+ 新建菜品</el-button
        >
      </div>
    </div>
    <div class="body">
      <el-table
        :data="tableData"
        stripe
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="菜品名称" width="180" />
        <el-table-column prop="image" label="图片" width="180">
          <template v-slot="{ row }">
            <el-image
              :src="`${env}/common/download?name=${row.image}`"
              style="width: 40px; height: 40px"
            />
          </template>
        </el-table-column>
        <el-table-column prop="categoryName" label="菜品分类" />
        <el-table-column prop="price" label="售价">
          <template v-slot="{ row }"> ￥{{ row.price / 100 }} </template>
        </el-table-column>
        <el-table-column prop="status" label="售卖状态">
          <template v-slot="{ row }">
            {{ row.status == "0" ? "停售" : "启售" }}
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="最后操作时间" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-link type="primary" @click="$router.push({ name: 'EditDish' })"
              >修改</el-link
            >
            <el-divider direction="vertical" />
            <el-link type="primary" @click="statusHandle('0')">{{
              row.status == "1" ? "停售" : "启售"
            }}</el-link>
            <el-divider direction="vertical" />
            <el-link
              slot="reference"
              type="danger"
              @click="deleHandle('单删', row.id)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="footer">
      <el-pagination
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>
<script>
import { getDish, deleteDish, dishStatus } from "@/api/dish";
export default {
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      price: 0,
      input: "",
      dishState: "",
      checkList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      const {
        data: { records, total },
      } = await getDish({ ...this.pageInfo });
      this.tableData = records;
      this.total = total;
      this.price = records.price;
      this.image = records.image;
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.getList();
    },
    statusHandle(row) {
      const params = {};
      if (typeof row === "string") {
        if (this.checkList.length === 0) {
          this.$message.error("批量操作，请先勾选操作菜品！");
          return false;
        }
        params.id = this.checkList.join(",");
        params.status = row;
      } else {
        params.id = row.id;
        params.status = row.status ? "0" : "1";
      }
      this.dishState = params;
      this.$confirm("确认更改该菜品状态?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        // 起售停售
        dishStatus(this.dishState).then((res) => {
          if (res.code === 1) {
            this.$message.success("菜品状态已经更改成功！");
            this.getList();
          } else {
            this.$message.error(res.msg || "操作失败");
          }
        });
      });
    },
    deleHandle(type, id) {
      if (type === "批量" && id === null) {
        if (this.checkList.length === 0) {
          return this.$message.error("请选择删除对象");
        }
      }
      this.$confirm("确认删除该菜品, 是否继续?", "确定删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(() => {
        deleteDish(type === "批量" ? this.checkList.join(",") : id).then(
          (res) => {
            if (res.code === 1) {
              this.$message.success("删除成功！");
              this.getList();
            } else {
              this.$message.error(res.msg || "操作失败");
            }
          }
        );
      });
    },
    handleSelectionChange(val) {
      const checkArr = [];
      val.forEach((n) => {
        checkArr.push(n.id);
      });
      this.checkList = checkArr;
    },
  },
};
</script>

<style lang="scss" scoped>
.contain {
  .header {
    margin-top: 20px;
    margin-left: 20px;
    display: flex;
    justify-content: space-between;
    .el-input {
      width: 300px;
    }
    .header-right {
      margin-right: 30px;
      .el-link {
        font-size: 16px;
        margin-right: 10px;
        margin-left: 10px;
      }
      .el-button {
        font-size: 18px;
        margin-left: 15px;
      }
    }
  }

  .body {
    margin-top: 20px;
    margin-left: 20px;
  }

  .footer {
    margin-top: 22px;
    display: flex;
    justify-content: center;
  }
}
</style>
